package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.domain.dto.CheckSchemaDTO;
import com.heima.domain.pojo.CheckSchema;
import com.heima.mapper.CheckSchemaMapper;
import com.heima.service.CheckSchemaService;
import com.heima.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CheckSchemaServiceImpl extends ServiceImpl<CheckSchemaMapper, CheckSchema> implements CheckSchemaService {

    @Autowired
    private CheckSchemaMapper checkSchemaMapper;

    @Override
    public IPage<CheckSchema> selectAllandquery(CheckSchemaDTO dto) {
       //1.构建分页对象
        Page<CheckSchema> page=new Page<>(dto.getPage(),dto.getPageSize());
        //2.构建查询条件
        LambdaQueryWrapper<CheckSchema> queryWrapper= new LambdaQueryWrapper<>();
        //账号模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(dto.getName()),CheckSchema::getName,dto.getName())
                .eq(dto.getType()!=null,CheckSchema::getType,dto.getType());

        return checkSchemaMapper.selectPage(page,queryWrapper);
    }

    @Override
    public void add(CheckSchema checkSchema) {
        checkSchema.setCreatedTime(LocalDateTime.now());
        checkSchema.setCreatedBy(UserContext.getCurrentAccount());
        checkSchemaMapper.insert(checkSchema);
    }

    @Override
    public CheckSchema getId(Integer id) {
        return checkSchemaMapper.selectById(id);
    }

    @Override
    public void update(CheckSchema checkSchema) {
        checkSchema.setUpdateTime(LocalDateTime.now());
        checkSchemaMapper.updateById(checkSchema);
    }

    @Override
    public void deleteByIds(List<Integer> idList) {
        checkSchemaMapper.deleteByIds(idList);
    }


}
